package com.zhang.generateexcel.util;

import cn.hutool.core.date.DateTime;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Timestamp是北京时间
 * Date是格林威治时间
 */
public class TimeUtil {
    public static SimpleDateFormat getFormat(String format) {
        return new SimpleDateFormat(format);
    }

    public static SimpleDateFormat format_ymd = getFormat("yyyy-MM-dd");

    public static String getDateString(Date date) {
        return format_ymd.format(date);
    }

    public static Date fromString(String timeString) {
        try {
            if (StringUtil.isEmpty(timeString)) {
                return null;
            }
            return format_ymd.parse(timeString);
        } catch (Exception ex) {
            return null;
        }
    }

    public static int getDayInt(Date time) {
        return toHtDateTime(time).dayOfMonth();
    }

    public static Timestamp newDate(int year, int month, int day) {
        return Timestamp.valueOf(StringUtil.format("{}-{}-{} 00:00:00", year, month, day));
    }

    public static Timestamp newDate(int year, int month, int day, int hour, int minute, int second) {
        return Timestamp.valueOf(StringUtil.format("{}-{}-{} {}:{}:{}",
                year, month, day, hour, minute, second));
    }

    public static DateTime toHtDateTime(Date time) {
        return DateTime.of(time);
    }

    public static int getYearInt(Date time) {
        return toHtDateTime(time).year();
    }

    public static int getMonthInt(Date time) {
        return toHtDateTime(time).month() + 1;
    }

    public static Date getNow() {
        return new Date();
    }

    /**
     * 毫秒
     */
    public static final long oneDayTicks = 24L * 60 * 60 * 1000;

    public static Date getDayPlus(Date time, int day) {
        return new Date(time.getTime() + oneDayTicks * day);
    }

    /**
     * 月初
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getMonthBegin(int year, int month) {
        if (IntegerUtil.lt(month, 1) || IntegerUtil.gt(month, 12)) {
            //月份只能传1-12
            return null;
        }
        return newDate(year, month, 1);
    }

    /**
     * 月初
     *
     * @param date
     * @return
     */
    public static Date getMonthBegin(Date date) {
        return getMonthBegin(getYearInt(date), getMonthInt(date));
    }

    /**
     * 月初
     *
     * @return
     */
    public static Date getMonthBegin() {
        return getMonthBegin(getNow());
    }

    /**
     * 月末(取下个月一号减一天)
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getMonthEnd(int year, int month) {
        if (IntegerUtil.equals(month, 12)) {
            //如果是12月取下一年的第一天
            return getDayPlus(getMonthBegin(year + 1, 1), -1);
        }
        return getDayPlus(getMonthBegin(year, month + 1), -1);
    }

    /**
     * 拿到上一个月
     *
     * @return
     */
    public static Date getPrevMonth() {
        //当前月初减一天
        return getDayPlus(getMonthBegin(), -1);
    }

    /**
     * 月末
     *
     * @param date
     * @return
     */
    public static Date getMonthEnd(Date date) {
        return getMonthEnd(getYearInt(date), getMonthInt(date));
    }

    /**
     * 月末
     *
     * @return
     */
    public static Date getMonthEnd() {
        return getMonthEnd(getNow());
    }

    public static int getWeekDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK) - 1;
    }

    public static String toString(Integer value) {
        if (value == null) {
            return "";
        }
        switch (value) {
            case 1:
                return "一";
            case 2:
                return "二";
            case 3:
                return "三";
            case 4:
                return "四";
            case 5:
                return "五";
            case 6:
                return "六";
            default:
                return "日";
        }
    }

    public static String getYearMonth(Date time) {
        return StringUtil.format(getYearInt(time)) +
                "-" +
                StringUtil.format(getMonthInt(time));
    }


    public static String getYearMonth() {
        return StringUtil.format(getYearInt(getNow())) +
                "-" +
                StringUtil.format(getMonthInt(getNow()));
    }


}
